package com.hxkj.service.impl;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hxkj.dao.LabelExtDao;
import com.hxkj.dao.LabelManagementDao;
import com.hxkj.domain.po.*;
import com.hxkj.service.LabelManagementService;
import com.platform.common.domain.result.ResultDataStruct;
import com.platform.common.domain.result.ResultMessageStruct;
import com.platform.common.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 标签管理实现类
 * <p>
 * Created by yaosy on 2018年7月19日
 */
@Service
@Slf4j
public class LabelManagementServiceImpl implements LabelManagementService {

    private static final Logger LOGGER = LoggerFactory.getLogger(LabelManagementServiceImpl.class);

    @Autowired
    private LabelManagementDao labelManagementDao;

    @Autowired
    private LabelExtDao labelExtDao;



    @Override
    public ResultMessageStruct save(LabelTags labelTags) {
        try{
            //查询标签管理是否存在相同标签名
            LabelManagement labelManagement = new LabelManagement();
            labelManagement.setLabelName(labelTags.getName());
            List<LabelManagement> labelManagementList =  labelManagementDao.get(labelManagement);
            if(labelManagementList != null && labelManagementList.size()>0){
                //throw new CustomException(,"");
            }
            /***保存标签管理***/
            labelManagement = new LabelManagement();
            JSONArray jsonArray =  labelTags.getJsonArray();
            updateOrInsert(jsonArray,labelTags,labelManagement);
        }catch(Exception e){
            e.printStackTrace();
            throw e;
        }
        return null;
    }

    @Override
    public ResultDataStruct<LabelTags> get(LabelTags labelTags) throws Exception {
        ResultDataStruct<LabelTags> labelTagsResultDataStruct = new ResultDataStruct<LabelTags>();
        try{
            long labelId = labelTags.getLabelId();
            LabelManagement labelManagement =  labelManagementDao.get(labelId);
            LabelExt labelExt = new LabelExt();
            labelExt.setLabelId(labelManagement.getLabelId());
            List<LabelExt> labelExtList =  labelExtDao.get(labelExt);
            LabelTags labelTags1 =  convert(labelManagement,labelExtList);
            labelTagsResultDataStruct.setData(labelTags1);
            labelTagsResultDataStruct.setCode(null);
        }catch(Exception e){
            e.printStackTrace();
            labelTagsResultDataStruct.setCode(302);
            labelTagsResultDataStruct.setMessage("查询数据失败!");
            return labelTagsResultDataStruct;
        }
        return labelTagsResultDataStruct;
    }

    protected LabelTags convert(LabelManagement labelManagement , List<LabelExt> labelExts){
        LabelTags labelTags = new LabelTags();
        labelTags.setLabelName(labelManagement.getLabelName()==null?"":labelManagement.getLabelName());
        labelTags.setSources(labelManagement.getSources()==null?"":labelManagement.getSources());
        labelTags.setComment(labelManagement.getComment()==null?"":labelManagement.getComment());
        labelTags.setUpdateTime(labelManagement.getUpdateTime());
        labelTags.setModifyBy(labelManagement.getModifyBy());
        labelTags.setCreateTime(labelManagement.getCreateTime());
        labelTags.setCreateor(labelManagement.getCreateor());
        labelTags.setSiteId(labelManagement.getSiteId());
        labelTags.setName(labelManagement.getName()==null?"":labelManagement.getName());
        labelTags.setLabelId(labelManagement.getLabelId());
        JSONArray jsonArray = new JSONArray();
        JSONObject jsonObject = new JSONObject();
        for(LabelExt la :labelExts){
            jsonObject = new JSONObject();
            jsonObject.put("labelExtId", la.getLabelExtId());
          /*  jsonObject.put("labelname", la.getName()==null?"":la.getName());
            jsonObject.put("publishingPlatform", la.getPublishingPlatform());
            jsonObject.put("templetName", la.getTempletName()==null?"":la.getTempletName());
            jsonObject.put("number", la.getNumber());*/
            jsonObject.put("usage", la.getUsageDesc()==null?"":la.getUsageDesc());
            jsonArray.add(jsonObject);
        }
        return labelTags;
    }

    @Override
    public ResultDataStruct<List<LabelTags>> getLabelTags(LabelTags labelTags) throws Exception {
        try{

        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ResultMessageStruct modify(LabelTags labelTags) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
           LabelManagement labelManagement =  labelManagementDao.get(labelTags.getLabelId());
           updateOrInsert(labelTags.getJsonArray(), labelTags, labelManagement);
        }catch(Exception e){
            e.printStackTrace();
            resultMessageStruct.setCode(403);
            resultMessageStruct.setMessage("修改数据失败!");
            return resultMessageStruct;
        }
        resultMessageStruct.setCode(null);
        return resultMessageStruct;
    }

    /***
     * 修改保存使用
     * **/
    protected void updateOrInsert(JSONArray jsonArray,LabelTags labelTags, LabelManagement labelManagement){
        if(StringUtils.isNotBlank(labelTags.getLabelName())) labelManagement.setLabelName(labelTags.getLabelName());
        if(StringUtils.isNotBlank(labelTags.getSources())) labelManagement.setSources(labelTags.getSources());
        if(StringUtils.isNotBlank(labelTags.getComment())) labelManagement.setComment(labelTags.getComment());
        if(labelTags.getUpdateTime() != null) labelManagement.setUpdateTime(System.currentTimeMillis());
        if(labelTags.getModifyBy() != null) labelManagement.setModifyBy(labelTags.getModifyBy());
        if(labelTags.getCreateTime() != null)labelManagement.setCreateTime(labelTags.getCreateTime());
        if(labelTags.getCreateor() != null)labelManagement.setCreateor(labelTags.getCreateor());
        if(labelTags.getSiteId() != null)labelManagement.setSiteId(labelTags.getSiteId());
        if(StringUtils.isNotBlank(labelTags.getName()))labelManagement.setName(labelTags.getName());
        if(labelTags.getLabelId() ==null) {
            labelManagementDao.save(labelManagement);
        }else {
            labelManagementDao.update(labelManagement);
            //删除拓展数据;再新增
            LabelExt labelExt = new LabelExt();
            labelExt.setLabelId(labelTags.getLabelId());
            List<LabelExt> labelExtList =  labelExtDao.get(labelExt);
            if(labelExtList != null && labelExtList.size() >0){
                for(LabelExt la  : labelExtList){
                    labelExtDao.delete(la);
                }
            }
        }

        if(jsonArray != null && jsonArray.size()>0){
            LabelExt labelExt = new LabelExt();
            for(int i = 0 ; i < jsonArray.size() ; i++){
                JSONObject jsonObject =  (JSONObject)jsonArray.get(i);
                labelExt = new LabelExt();
                labelExt.setCreateor(labelTags.getCreateor());
                labelExt.setModifyBy(labelTags.getModifyBy());
                labelExt.setDataType(labelTags.getDataType());
                labelExt.setName(labelTags.getLabelName());
                /*labelExt.setPublishingPlatform(labelTags.getPublishingPlatform());
                labelExt.setNumber(labelTags.getNumber());
                labelExt.setLabelId(labelManagement.getLabelId());
                labelExt.setTempletName(labelTags.getTempletName());*/
                labelExt.setUsageDesc(labelTags.getUsage());
                labelExtDao.save(labelExt);
            }
        }
    }


    @Override
    public ResultMessageStruct remove(LabelTags labelTags) throws Exception {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try{
            LabelManagement labelManagement = new LabelManagement();
            labelManagement.setSiteId(labelTags.getSiteId());
            labelManagement.setLabelId(labelTags.getLabelId());
            labelManagement.setDelTag(1);
            labelManagementDao.update(labelManagement);
            //根据labelManagement获取拓展表
            LabelExt labelExt = new LabelExt();
            labelExt.setLabelId(labelTags.getLabelId());
            List<LabelExt> labelExtList =  labelExtDao.get(labelExt);
            if(labelExtList != null && labelExtList.size() >0){
                for(LabelExt la  : labelExtList){
                    labelExtDao.delete(la);
                }
            }
        }catch(Exception e){
            e.printStackTrace();
            resultMessageStruct.setMessage("删除数据失败!");
            resultMessageStruct.setCode(303);
            return resultMessageStruct;
        }
        resultMessageStruct.setCode(null);
        return resultMessageStruct;
    }
}
